package exer;

/**反转单链表
 * @author shkstart
 * @create 2022-08-13-22:56
 */
public class Num206 {
    public ListNode reverseList(ListNode head) {
//        if (head == null || head.next == null) {
//            return head;
//        }
//        ListNode newHead = null;
//        while (head != null) {
//            ListNode node = new ListNode(head.val);
//            node.next = newHead;
//            newHead = node;
//            head = head.next;
//        }
//        return newHead;



        if (head == null || head.next == null) {
            return head;
        }
        ListNode perv=null;
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = perv;
            perv =cur;
            cur = next;
        }
        return perv;


        //递归
    }
}
